package com.sky.service.impl;

import com.sky.domain.Tag;
import com.sky.mapper.TagMapper;
import com.sky.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 尹稳健~
 * @version 1.0
 */
@Service
public class TagServiceImpl implements TagService {

    @Autowired
    private TagMapper tagMapper;


    @Override
    public List<Tag> getAllTag() {
        return tagMapper.getAllTag();
    }

    @Override
    public Tag getTagByName(String name) {
        return tagMapper.getTagByName(name);
    }

    @Override
    public void saveTag(Tag tag) {
        tagMapper.saveTag(tag);
    }

    @Override
    public Tag getTag(Long id) {
        return tagMapper.getTag(id);
    }

    @Override
    public void updateTag(Tag tag) {
        tagMapper.updateTag(tag);
    }

    @Override
    public void deleteTag(Long id) {
        tagMapper.deleteTag(id);
    }

    //把前端的tagIds字符串转换为list集合
    private List<Long> convertToList(String ids) {
        List<Long> list = new ArrayList<>();
        if (!"".equals(ids) && ids != null) {
            String[] idarray = ids.split(",");
            for (int i = 0; i < idarray.length; i++) {
                list.add(new Long(idarray[i]));
            }
        }
        return list;
    }
    @Override
    public List<Tag> getTagByString(String Text) {
        List<Tag> tags = new ArrayList<>();
        List<Long> longs = convertToList(Text);
        for (Long long1 : longs) {
            tags.add(tagMapper.getTag(long1));
        }
        return tags;
    }

    @Override
    public List<Tag> getBlogTag() {
        return tagMapper.getBlogTag();
    }
}
